<?php

namespace app\backend\controller;

use think\Controller;
use think\Request;
use think\Session;
use think\Db;
use app\backend\model\User as UserModel;
use app\backend\model\Permission as PermissionModel;
class User extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
        // 当前请求的页数
        $page = $request->param('page');
        if (empty($page)){
            $page = 1;
        }
        // 文章总数
        $user_count = UserModel::count();  
        // 每页显示多少条
        $limit = 10;
        // 偏移量  从哪一条开始算起
        $offset = ($page - 1) * $limit;
        // 总页数
        $page_count = intval(ceil($user_count/$limit));
        //SQL  
        $res_users = UserModel::order('create_time desc')->limit($offset, $limit)->select();
        $permissions = PermissionModel::select();
        $res = [$res_users, $page_count, $user_count, $limit, $permissions];
        return json_encode($res);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $username = $request->param('username');
        $user_1 = UserModel::where('name', $username)->find();
        if(!empty($user_1)){
            return 3;
            exit;
        }
        $password = $request->param('password');
        $permission_id = $request->param('permission_id');
        $gender = $request->param('gender');
        $date = $request->param('date');
        $intro = $request->param('intro');
        $email = $request->param('email');
        $inkey = md5($request->param('username'));
        $user = new UserModel;
        $user->name = $username;
        $user->inkey = $inkey;
        $user->password = $password;
        $user->permission_id = $permission_id;
        $permission = PermissionModel::where('id', $permission_id)->find();
        $permission->num += 1; 
        $permission->save();
        if (!empty($intro)){
            $user->intro = $intro;
        }
        $user->email = $email;
        $user->gender = $gender;
        $user->date = $date;
        if($user->save()){
            return 1;
        }else{
            return 0;
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        if(Session::has('username')){
        }else{
            return 'You Cant Not Found.';
            exit;
        }
        $user = UserModel::get($id);
        return json_encode($user);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        if (Session::has('username')){
            if(Session::get('permission') != '管理者'){
                return 0;
                exit;
            }
        }
        $name = $request->param('name');
        $permission_id = $request->param('permission_id');
        $email = $request->param('email');
        if ($name == null || $permission_id == null || $email == null){
            return 2;
        }
        $user_1 = UserModel::where('name', $name)->find();
        if(!empty($user_1)){
            $permission_id_old = $user_1->permission_id;
            $user_1->permission_id = $permission_id;
            $permission = PermissionModel::where('id', $permission_id_old)->find();
            $permission->num -= 1;
            $permission->save();
            $permission = PermissionModel::where('id', $permission_id)->find();
            $permission->num += 1;
            $permission->save();
            $user_1->email = $email;
            $user_1->save();
            return 1;
            exit;
        }

        $user = UserModel::get($id);
        $user->name = $name;
        $user->permission_id = $permission_id;
        $user->email = $email;
        if($user->save()){
            return 1;
        }else{
            return 3;
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        if(Session::has('username')){
            if (Session::get('permission') == '管理者'){
                $user = UserModel::where('id', $id)->find();
                $permission = PermissionModel::where('id', $user->permission_id)->find();
                $permission->num -= 1;
                $permission->save();
                $res = UserModel::destroy($id);
                if ($res){
                    return '删除用户成功！';
                }
            }
        }else{
            return 0;
        }
    }
}
